package org.springblade.modules.repair.vo;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanContentUser;
import org.springblade.modules.repair.entity.MaintainPlanTemplate;
import org.springblade.modules.repair.enums.EmerProcTypeEnum;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.WorkOrderTypeEnum;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date 11:44 2022/11/1
 * @Description {
 *
 * }
 * @Author lsay
 **/
@Data
public class PlanDataPageVO {

	/**
	 * 任务id
	 */
	@JsonSerialize(using = ToStringSerializer.class)
	private Long planId;

	@JsonSerialize(using = ToStringSerializer.class)
	private Long elevatorId;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date planDate;

	private String elevatorAddress;

	private String driveModeName;

	@JsonSerialize(using = ToStringSerializer.class)
	private Long buildingId;

	private String buildingName;

	private String planCheckName;

	private List<String> templateNames;

	private Integer workMinute = 0;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date planBeginTime;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date planEndTime;

	private Integer scoreStatus;

	private BigDecimal score;

	/**
	 * 任务类型
	 */
	private Integer typeStatus;
	private String typeStatusDesc;

	private Integer auditType;

	public void setTypeStatus(Integer typeStatus) {
		this.typeStatusDesc = MaintainPlanContentEnum.getName(typeStatus);
		this.typeStatus = typeStatus;
	}

	/**
	 * 任务状态 1-待开始 2-进行中 3-提交任务 4-签字完成
	 */
	private Integer status;

	private String statusDesc;

	/**
	 * 及时性 1-及时 2-逾期
	 */
	private String timeliness;

	/**
	 * 时长 1-过长 2-过短
	 */
	private String duration;

	/**
	 * 关联人员所属组名称
	 */
	private List<String> groupNames;
	/**
	 * 关联人员
	 */
	private List<MaintainPlanContentUser> planContentUserVos;

	/**
	 * 评分时间
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date scoreTime;

	/**
	 * 更新时间
	 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date updateTime;

	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	/**
	 * 0: 未延迟, 1: 延迟
	 */
	private Integer isDeferred;
	private String deferredDesc;

	public void setIsDeferred(Integer isDeferred) {
		this.isDeferred = isDeferred;
		if(ObjectUtil.isNotEmpty(isDeferred)){
			if(isDeferred.equals(1)){
				this.deferredDesc = "延期保养";
			}
		}
	}

	private String mfDesc;

	private String problemItem;
	private String problemOtherDesc;

	/**
	 * 电梯状态： 1-正常,2-停梯
	 */
	private Integer elevatorStop;

	/**
	 * 故障现象
	 */
	private List<String> problemItemNames;

	/**
	 * 部件名称集合
	 */
	private String partsName;

	/**
	 * 部件数量
	 */
	private Integer num;


	private String buildingAddress;


	private Integer deferType;

	private Date maintainSignatureTime;
	private Date planOverdueDate;
	private Date elevatorOverdueDate;

	private String overdueName;

	/**
	 * 紧急程度
	 */
	private Integer emerProcType;

	/**
	 * 工单类型
	 */
	private Integer workOrderType;

	/**
	 * 紧急程度描述
	 */
	private String emerProcTypeName;

	/**
	 * 工单类型描述
	 */
	private String workOrderTypeName;

	/**
	 * 用户部门集合
	 */
	private String userDeptNames;

	@ApiModelProperty("评分类型1:有效订单，2：无效工单3：无法判断")
	private Integer scoreType;

	private Integer overdueType;

	private String overdueTypeName;

	public void setOverdueType(Integer overdueType) {
		if(Func.isNotEmpty(overdueType)){
			if (overdueType == 1){
				this.overdueTypeName = "未逾期";
			}
			if (overdueType == 2){
				this.overdueTypeName = "明天即将逾期";
			}
			if (overdueType == 3){
				this.overdueTypeName = "已逾期";
			}
		}
		this.overdueType = overdueType;
	}

	public void setPartsName(String partsName) {
		if(Func.isNotEmpty(partsName)){
			this.partsName = partsName.replace("&amp;&amp;",">");
		}
	}

	public void setEmerProcType(Integer emerProcType) {
		if(Func.isNotEmpty(emerProcType)){
			this.emerProcTypeName = EmerProcTypeEnum.getName(emerProcType);
		}
		this.emerProcType = emerProcType;
	}

	public void setWorkOrderType(Integer workOrderType) {
		if(Func.isNotEmpty(workOrderType)){
			this.workOrderTypeName = WorkOrderTypeEnum.getName(workOrderType);
		}
		this.workOrderType = workOrderType;
	}


	public void setPlanCheckName(String planCheckName) {
		if(Func.isNotEmpty(planCheckName) && planCheckName.contains("%")){
			List<String> strings = Func.toStrList(planCheckName);
			List<MaintainPlanTemplate> list = new ArrayList<>(10);
			for (String str :strings) {
				String[] split = str.split("%");
				MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
				maintainPlanTemplate.setTemplateName(split[1]);
				maintainPlanTemplate.setSort(Convert.toInt(split[2],0));
				list.add(maintainPlanTemplate);
			}
			List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
			this.planCheckName = String.join(",",collect);
			this.templateNames = collect;
		}else{
			this.planCheckName = planCheckName;
		}
	}
}
